/**
 * 文件：activity_Task_Detailed_List.java
 * 包名：com.ronri.ams.task;   --- 正式发布时需要修正
 * 说明：任务信息列表
 * 作者：MS During
 * 日期：2017/5/6
 * 历史：
 */
package com.ronri.ams.task;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.cz.nongyetongv5.R;
import com.cz.nongyetongv5.application.InitApplication;
import com.ronri.adapter.CommonAdapter;
import com.ronri.ams.SystemEnvironmental;
import com.ronri.ams.bean.taskListBean;
import com.ronri.ams.bean.tb_sampleinfo;
import com.ronri.ams.sample.abstract_activity;
import com.ronri.common.DateUtil;
import com.ronri.common.UrlEncode;
import com.ronri.common.WaitDialog;
import com.ronri.db.RemoteDataService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.cz.nongyetongv5.application.InitApplication.gSampleInfo;


public class activity_Task_Detailed extends abstract_activity {

    // 列表数据
    private List<Map<String, Object>> mItems = new ArrayList<Map<String, Object>>();

    // 任务信息
    private taskListBean mTaskListBean = null;

    // 返回按钮
    private RelativeLayout btnBack = null;

    // 列表视图
    private ListView mListView = null;

    private Context mContext = null;

    private int mProject_id = -1;
    private int mNature = 1;
    private String mDeptName = "";
    private String mEndDate = "";
    private  int mType = 1;

    private TextView mAlready = null;
    private TextView mWait = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.task_detailed_list);
        Log.i("aaa","activity_Task_Detailed");
        btnBack = (RelativeLayout) findViewById(R.id.btnBack);

        btnBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        setTitle("", "");
        // CreateDemoData();

        TextView textView = (TextView) findViewById(R.id.title_text_tv);
        textView.setText("任务列表");

        mAlready = (TextView)findViewById(R.id.bt_task_already);
        mWait = (TextView)findViewById(R.id.bt_task_wait);

        Bundle bundle = getIntent().getExtras();

        if (bundle != null) {
            mProject_id = bundle.getInt("project_id");
            mNature = bundle.getInt("nature");
            mDeptName = bundle.getString("dept_name");
            mEndDate = bundle.getString("end_date");
            mType = bundle.getInt("type");
        }

        mContext = this;

        mListView = (ListView) findViewById(R.id.listTaskDetail);

        getRemoteData();
    }


    /***
     * 添加后保存
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            if (requestCode == SystemEnvironmental.Sample_MAIN) {
                if (data != null) {
                    String szJson = JSON.toJSONString(gSampleInfo);
                    tb_sampleinfo sinfo = new tb_sampleinfo();
                    sinfo.setPid(gSampleInfo.getProject_id());
                    sinfo.setTid(gSampleInfo.getTask_id());
                    sinfo.setDtime(DateUtil.getCurrentDate("yyyy-MM-dd"));
                    sinfo.setEntname(gSampleInfo.getInspectd_name());
                    sinfo.setStatus("0");
                    sinfo.setHumen(gSampleInfo.getSample_user());
                    sinfo.setTname(gSampleInfo.getTask_name());
                    sinfo.setPname(gSampleInfo.getProject_name());
                    sinfo.setSamplename(gSampleInfo.GetSamplebreed());
                    sinfo.setJson(szJson);

                    sinfo.save();
                }
            }
        }
    }

    /****
     * 获取远程数据
     */
    public void getRemoteData() {

        RemoteDataService remoteDataService = new RemoteDataService();

        // 获取远程地址
        String szRemoteAddress = InitApplication.SystemConfig.getBase().RemoteAddress(
                InitApplication.SystemConfig.getModule().getTlist().getAddress());

        Map<String, Object> mParam = new HashMap<String, Object>();

        try {
            mParam.put("addFrom", 1);
            mParam.put("addMark", "android");
            mParam.put("page", 1);
            mParam.put("pageSize", 5);
            mParam.put("user_id", InitApplication.LoginInfo.getData().getUser_id());
            mParam.put("detector_id", InitApplication.LoginInfo.getData().getDetectorid());
            mParam.put("project_id", mProject_id);
            mParam.put("nature", mNature);
            mParam.put("dept_name", mDeptName);
            mParam.put("end_date", mEndDate);

        } catch (Exception ex) {

        }

        // 获取远程数据
        remoteDataService.getRemoteData(szRemoteAddress,
                UrlEncode.getUrlParamsByMap(mParam, false),
                InitApplication.SystemConfig.getModule().getTdetail().getMethod(),
                new RemoteDataService.OnRemoteDataEvent() {
                    // 返回结果
                    @Override
                    public void OnResultEvent(String szString) {
                        if (szString.isEmpty())
                            return;

                        try {
                            mTaskListBean = JSON.parseObject(szString, taskListBean.class);

                            if (mTaskListBean.isSuccess()) {
                                //mAlready.setText(mTaskListBean.getData().getTotal());

                                if (mListView != null) {
                                    // 获取List视图
                                    mListView = (ListView) findViewById(R.id.listTaskDetail);
                                    mListView.setAdapter(new SimpleAdapterWithEvent(
                                            mContext,
                                            R.layout.v_sample_detail_info,
                                            mTaskListBean,
                                            new int[]{R.id.bt_do_sample},
                                            new OnControlClickEvent() {
                                                @Override
                                                public void OnClick(View view, int Target) {

                                                    // 获取位置，便于读取数据
                                                    int postion = (Integer) view.getTag();

                                                    switch (Target) {
                                                        case R.id.bt_do_sample: {
                                                           if (mType == 1){
                                                               Intent intent = new Intent(mContext, activity_task_sample_detail.class);
                                                               Bundle bundle = new Bundle();
                                                               bundle.putInt("project_id", mProject_id);
                                                               bundle.putInt("task_id", mTaskListBean.getData().getRows().get(postion).getTask_id());
                                                               intent.putExtras(bundle);
                                                               startActivity(intent);
                                                           }else {
                                                               Intent intent = new Intent(mContext, activity_task_detect_detail.class);
                                                               Bundle bundle = new Bundle();
                                                               bundle.putInt("project_id", mProject_id);
                                                               bundle.putInt("task_id", mTaskListBean.getData().getRows().get(postion).getTask_id());
                                                               intent.putExtras(bundle);
                                                               startActivity(intent);
                                                           }
                                                        }
                                                        break;
                                                    }
                                                }
                                            }
                                    ));
                                }
                            }
                        } catch (Exception ex) {

                        }
                    }

                    // 状态控制，是否显示加载过程
                    @Override
                    public void OnRuningState(int nStatus) {
                        if (nStatus == SystemEnvironmental.StartGetDataFromServer) {
                            WaitDialog.ShowDialog(mContext, "请稍后...");
                        } else {
                            WaitDialog.HideDialog();
                        }
                    }

                    @Override
                    public String OnGetCustomParam() {
                        return null;
                    }
                }
        );
    }

    class taskAdapter extends CommonAdapter<taskListBean.DataBean.RowsBean> {

        /****
         * 构造函数
         * @param context
         * @param layoutId
         * @param mDatas
         */
        public taskAdapter(Context context, int layoutId, List<taskListBean.DataBean.RowsBean> mDatas) {
            super(context, layoutId, mDatas);
        }

        @Override
        public void addDataToView(taskListBean.DataBean.RowsBean rowsBean, View o) {

        }

        @Override
        public String FormatData(String szField, String szValue) {
            return null;
        }
    }
}
